const { exec } = require('child_process');

const getLocalIP = require('./src/utils/getLocalIP')
const port = process.env.PORT || 3000;
const localIP = getLocalIP();

let http =
  `server to http://localhost:${port}
server to http://${localIP}:${port}
DOCS to http://localhost:${port}/api-docs
DOCS to http://localhost:${port}/redoc`


function runCommand(command) {
  exec(command, (error, stdout, stderr) => {
    if (error) {
      console.error(`Error executing command: ${command}`);
      console.error(error);
      return;
    }
    console.log(`Output of command: ${command}`);
    console.log(stdout);
  });
}

function startServer() {
  // Check if pm2 is installed
  exec('where pm2 || which pm2', (error) => {
    if (!error) {
      console.log("Using PM2 to start the server...");
      console.log(http);
      runCommand('pm2 start app.js');
      return;
    }

    // Check if nodemon is installed
    exec('where nodemon || which nodemon', (error) => {
      if (!error) {
        console.log("Using Nodemon to start the server...");
        console.log(http);
        runCommand('nodemon app.js');
        return;
      }

      // Fallback to using Node
      console.log("Using Node to start the server...");
      console.log(http);
      runCommand('node app.js');
    });
  });
}

startServer();